package test;

import anaydis.sort.Sorter;
import sort.benchmark.Benchmark;
import sort.data.IntegerDataSetGenerator;
import sort.impl.*;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

public class SorterTest4 {

    public static void printSorters(int n) {
        IntegerDataSetGenerator generator = new IntegerDataSetGenerator();
        Benchmark benchmark = new Benchmark();

        QuickSorterCutOff quickSorterCutOff = new QuickSorterCutOff();
        quickSorterCutOff.addSorterListener(benchmark);

        printReport(generator, quickSorterCutOff, "QuickCutOffSorted", benchmark, n);
    }

    private static void printReport(IntegerDataSetGenerator idsg, Sorter sorter, String s, Benchmark benchmark, int n) {
        List<Integer> ascendingList = idsg.createAscending(n);
        printList(ascendingList, "Ascending List", "Disordered");
        long timeAB1 = System.currentTimeMillis();
        sorter.sort(idsg.getComparator(), ascendingList);
        long timeAB2 = System.currentTimeMillis();
        printList(ascendingList, "Ascending List", s);
        System.out.println("Comparisons: " + benchmark.getComparisons());
        System.out.println("Swaps: " + benchmark.getSwaps());
        System.out.println("Time used to sort: " + (timeAB2 - timeAB1));

        List<Integer> descendingList = idsg.createDescending(n);
        printList(descendingList, "Descending List", "Disordered");
        long timeDB1 = System.currentTimeMillis();
        sorter.sort(idsg.getComparator(), descendingList);
        long timeDB2 = System.currentTimeMillis();
        printList(descendingList, "Descending List", s);
        System.out.println("Comparisons: " + benchmark.getComparisons());
        System.out.println("Swaps: " + benchmark.getSwaps());
        System.out.println("Time used to sort: " + (timeDB2 - timeDB1));

        List<Integer> randomList = idsg.createRandom(n);
        printList(randomList, "Random List", "Disordered");
        long timeRB1 = System.currentTimeMillis();
        sorter.sort(idsg.getComparator(), randomList);
        long timeRB2 = System.currentTimeMillis();
        printList(randomList, "Random List", s);
        System.out.println("Comparisons: " + benchmark.getComparisons());
        System.out.println("Swaps: " + benchmark.getSwaps());
        System.out.println("Time used to sort: " + (timeRB2 - timeRB1));
    }

    private static void printList(List<Integer> list, String listType, String sorter) {
        System.out.println(listType);
        System.out.println(sorter);
        for (Integer aList : list) {
            System.out.println(aList);
        }
    }

    public static String text(String message) {
        try {
            System.out.println(message);

            return new BufferedReader(
                    new InputStreamReader(System.in)).readLine();
        }
        catch (IOException e) {
            System.out.println(e);
            System.exit(1);
            return e.toString();
        }
    }

    public static int getInt(String message) {
        return new Integer(text(message));
    }

    public static void main(String[] args) {
        int n = 0;
        while (n != 1) {
            n = SorterTest.getInt("Ingrese el n deseado");
            printSorters(n);
        }
    }
}
